package com.xtx.air.web.action.user;

import com.xtx.air.bo.role.IRoleBO;
import com.xtx.air.bo.user.IUserBO;
import com.xtx.air.dataobject.CodeDO;
import com.xtx.air.dataobject.RoleDO;
import com.xtx.air.dataobject.UserDO;
import com.xtx.air.web.action.BaseAction;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.util.ArrayList;
import java.util.List;

/**
 * User: hanyi
 * Date: 12-2-9
 * Time: 下午8:55
 */
public class UserAction extends BaseAction{
    private IUserBO userBO;
    private IRoleBO roleBO;
    private UserDO query;
    private List<UserDO> userList;
    private UserDO user;


    public String view(){
        return SUCCESS;
    }

    @RequiresPermissions("user:view")
    public String list(){
        userList = userBO.findUser(query);
        return SUCCESS;
    }

    @RequiresPermissions("user:changeRole")
    public String changeRole(){
        try{
            userBO.updateRoleUserRef(query);
            addActionError("更新用户角色成功");
        }catch (Exception e){
            addActionError("更新用户角色失败");
        }
        return SUCCESS;
    }

    public String viewModify(){
        user = userBO.getUserById(user.getId());
        return SUCCESS;
    }

    @RequiresPermissions("user:resetPassword")
    public String resetUserPass(){
        try{
            userBO.resetUserPass(user.getId());
            addActionError("密码已经成功发至用户邮箱");
        }catch (Exception e){
            addActionError("密码重置失败:"+e.getMessage());
        }
        viewModify();
        return SUCCESS;
    }

    @RequiresPermissions("user:modify")
    public String modifyUser(){
        try{
            userBO.updateUserInfo(user);
            addActionError("用户资料修改成功");
        }catch (Exception e){
            addActionError("用户资料修改失败:"+e.getMessage());
        }
        viewModify();
        return SUCCESS;
    }

    

    public IUserBO getUserBO() {
        return userBO;
    }

    public void setUserBO(IUserBO userBO) {
        this.userBO = userBO;
    }

    public List<UserDO> getUserList() {
        return userList;
    }

    public void setUserList(List<UserDO> userList) {
        this.userList = userList;
    }

    public UserDO getQuery() {
        return query;
    }

    public void setQuery(UserDO query) {
        this.query = query;
    }

    public void setRoleBO(IRoleBO roleBO) {
        this.roleBO = roleBO;
    }

    public UserDO getUser() {
        return user;
    }

    public void setUser(UserDO user) {
        this.user = user;
    }

    public List<CodeDO> getRoleList(){
        RoleDO roleDO = new RoleDO();
        roleDO.setType(0);//前台角色
        List<RoleDO> list = roleBO.findRole(roleDO);
        List<CodeDO> codeList = new ArrayList<CodeDO>();
        for(RoleDO role : list){
            CodeDO code = new CodeDO(role.getRoleId(),role.getRoleName());
            codeList.add(code);
        }
        return codeList;
    }
}
